package com.wrb.pwd.utils;

import javax.crypto.Cipher;
import java.nio.charset.StandardCharsets;
import java.security.Key;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;

/**
 * 对文本进行RSA加密
 * 参考：https://www.jianshu.com/p/316ce9fa083e
 *
 * @author wrb
 * @date 2019/10/11 17:10
 */
public class SecurityRSA {

	public static final String KEY_ALGORITHM = "RSA";

	private static final String PUBLIC_KEY = "RSAPublicKey";
	private static final String PRIVATE_KEY = "RSAPrivateKey";

	private static Map<String, Object> keyMap;

	static {
		//初始化密钥
		try {
			keyMap = initKey();
		} catch (Exception e) {
			keyMap = null;
		}
	}

	/**
	 * 初始化密钥
	 *
	 * @return
	 * @throws Exception
	 */
	public static Map<String, Object> initKey() throws Exception {
		//生成公钥和私钥对，基于RSA算法生成对象
		KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(KEY_ALGORITHM);

		//初始化密钥对生成器，密钥大小为1024位
		keyPairGen.initialize(1024);

		//生成一个密钥对，保存在keyPair中
		KeyPair keyPair = keyPairGen.generateKeyPair();

		//得到私钥和公钥
		RSAPrivateKey privateKey =(RSAPrivateKey) keyPair.getPrivate();
		RSAPublicKey publicKey = (RSAPublicKey)keyPair.getPublic();

		Map<String, Object> keyMap = new HashMap<String, Object>(2);

		keyMap.put(PUBLIC_KEY, publicKey);
		keyMap.put(PRIVATE_KEY, privateKey);
		return keyMap;
	}
	/**
	 * 取得私钥
	 *
	 * @return
	 */
	public static String getPrivateKey() {
		Key key = (Key) keyMap.get(PRIVATE_KEY);
		return base64Encode2String(key.getEncoded());
	}

	/**
	 * 取得公钥
	 *
	 * @return
	 */
	public static String getPublicKey() {
		Key key = (Key) keyMap.get(PUBLIC_KEY);
		return base64Encode2String(key.getEncoded());
	}

	public static String base64Encode2String(byte[] input) {
		return Base64.getEncoder().encodeToString(input);
	}

	/**
	 * 加密
	 *
	 * @param data
	 * @param key
	 * @return
	 * @throws Exception
	 */
	public static byte[] encryptByPublicKey(byte[] data, String key)
			throws Exception {
		// 对公钥解密
		byte[] keyBytes = base64Decode(key);

		// 取得公钥
		X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes);
		KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
		Key publicKey = keyFactory.generatePublic(x509KeySpec);

		// 对数据加密
		Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
		cipher.init(Cipher.ENCRYPT_MODE, publicKey);

		return cipher.doFinal(data);
	}

	/**
	 * 解密
	 *
	 * @param data
	 * @param key
	 * @return
	 * @throws Exception
	 */
	public static byte[] decryptByPrivateKey(byte[] data, String key)
			throws Exception {
		// 对密钥解密
		byte[] keyBytes = base64Decode(key);

		// 取得私钥
		PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);
		KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
		Key privateKey = keyFactory.generatePrivate(pkcs8KeySpec);

		// 对数据解密
		Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
		cipher.init(Cipher.DECRYPT_MODE, privateKey);

		return cipher.doFinal(data);
	}

	public static byte[] base64Decode(String input) {
		return Base64.getDecoder().decode(input);
	}

	public static void main(String[] args) throws Exception {
		String content = "";
		byte[] bytes = encryptByPublicKey(content.getBytes(), getPublicKey());
		System.out.println(new String(bytes));
		byte[] bytes1 = decryptByPrivateKey(bytes, getPrivateKey());
		System.out.println(new String(bytes1));
	}
}
